//
//  CardCollectionViewController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/5.
//  Copyright © 2018年 longlong. All rights reserved.
//

#import "CardCollectionViewController.h"
#import "CardCollectionViewFlowLayout.h"
#import "CardCollectionViewCell.h"

@interface CardCollectionViewController ()
{
    ///开始滑动时的 Y 值
    CGFloat _startSlidingY;
    ///向上滑动
    BOOL _upSliding;
    ///第一次运行
    BOOL _firstRun;
}
@property (nonatomic ,strong) NSMutableArray<UIImage *> *imageNameArray;
@end

@implementation CardCollectionViewController

static NSString * const reuseIdentifier = @"CoreCollectionViewCell";

- (instancetype)init
{
    CardCollectionViewFlowLayout *flatout = [[CardCollectionViewFlowLayout alloc] init];
    flatout.itemSize = CGSizeMake(ScreenWidth / 2.0, ScreenWidth / 2.0);
    return [self initWithCollectionViewLayout:flatout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[CardCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.view.backgroundColor=[UIColor colorWithRed:38/255.0f green:44/255.0f blue:50/255.0f alpha:1];
    self.collectionView.contentInset = UIEdgeInsetsMake((ScreenHeight - ScreenWidth) / 2.0, 0, (ScreenHeight - ScreenWidth) / 2.0, 0);
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.backgroundColor=[UIColor colorWithRed:38/255.0f green:44/255.0f blue:50/255.0f alpha:1];
    _firstRun=YES;
    _upSliding=YES;
    
    [self loadImageData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_firstRun) {
        /*第一次运行显示界面时 所有显示的cell都放到最高层 后出现的就会压住前面的*/
        //上
        [collectionView bringSubviewToFront:cell];
    } else {
        
        if (_upSliding) {
            /* 向上滑时 后出现的就会压住前面的*/
            //上滑
            [collectionView bringSubviewToFront:cell];
        } else {
            /* 向下滑时 把每个将要出现的放到最底部 后出现的就会在前一个cell的下面*/
            //下滑
            [collectionView sendSubviewToBack:cell];
        }
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageNameArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.imageView.image = self.imageNameArray[indexPath.item];
    return cell;
}

#pragma mark UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _startSlidingY = scrollView.contentOffset.y;
    _firstRun=NO;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y<_startSlidingY) {
        //下
        _upSliding=NO;
    } else  {
        //上
        _upSliding=YES;
    }
    _startSlidingY = scrollView.contentOffset.y;
}

- (void)loadImageData
{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableArray *array = [NSMutableArray array];
        for (int i = 1; i < 10; i ++)
        {
            NSString *name = [NSString stringWithFormat:@"collection_%02d",i];
            NSString *path = [kResourceBundle pathForResource:name ofType:@"jpg"];

            UIImage *image = [UIImage imageWithContentsOfFile:path];
            [array addObject:image];
        }

        [_imageNameArray addObjectsFromArray:array];
        [_imageNameArray addObjectsFromArray:array];
        [_imageNameArray addObjectsFromArray:array];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
        
    });
    
    
    
}


#pragma mark - Getters And Setters

- (NSMutableArray<UIImage *> *)imageNameArray
{
    if (_imageNameArray == nil)
    {
        _imageNameArray = [NSMutableArray array];
    }
    
    return _imageNameArray;
}

@end

